package io.wit.modules.sys.controller;

import io.wit.common.annotation.SysLog;
import io.wit.common.utils.PageUtils;
import io.wit.common.utils.Query;
import io.wit.common.utils.R;
import io.wit.common.validator.ValidatorUtils;
import io.wit.config.ConfigFile;
import io.wit.modules.sys.entity.SysRoleEntity;
import io.wit.modules.sys.service.SysRoleMenuService;
import io.wit.modules.sys.service.SysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
*/
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {

    @Autowired
	private SysRoleService sysRoleService;

    @Autowired
	private SysRoleMenuService sysRoleMenuService;

    @Value("${platform_role_id}")
    private long role_id;
	
	/**
	 * 角色列表
	*/
	@RequestMapping("/list")
	@RequiresPermissions("sys:role:list")
	public R list(@RequestParam Map<String, Object> params){
		//如果不是超级管理员，则只查询自己创建的角色列表
		if(!ConfigFile.superUser.equals(getUserName())){
			params.put("createUserId", getUserId());
		}
		//查询列表数据
		final Query query = new Query(params);
		final List<SysRoleEntity> list = sysRoleService.queryList(query);
		final int total = sysRoleService.queryTotal(query);
		final PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
		return R.ok().put("page", page);
	}
	
	/**
	 * 角色列表
	*/
	@GetMapping("/select")
	@RequiresPermissions("sys:role:select")
	public R select(){
		final Map<String, Object> map = new HashMap<>();
        final String userName = getUserName();
        final Long roleId = getRoleId();
        if(ConfigFile.KEY_SUPER.equals(userName) || roleId == role_id){//显示所有的角色列表
            final List<SysRoleEntity> list = sysRoleService.getListRole(map);
            return R.ok().put("list",list).put("webmaster","webmaster");
        }else{
            return R.ok().put("list",new ArrayList<SysRoleEntity>());
        }
	}
	
	/**
	 * 角色信息
	*/
	@GetMapping("/info/{roleId}")
	@RequiresPermissions("sys:role:info")
	public R info(@PathVariable("roleId") Long roleId){
		final SysRoleEntity role = sysRoleService.queryObject(roleId);
		//查询角色对应的菜单
		final List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		return R.ok().put("role", role);
	}
	
	/**
	 * 保存角色
	*/
	@SysLog("保存角色")
	@RequestMapping("/save")
	@RequiresPermissions("sys:role:save")
	public R save(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);
		sysRoleService.save(role);
		return R.ok();
	}
	
	/**
	 * 修改角色
	 */
	@SysLog("修改角色")
	@RequestMapping("/update")
	@RequiresPermissions("sys:role:update")
	public R update(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);
		sysRoleService.update(role);
		return R.ok();
	}
	
	/**
	 * 删除角色
	 */
	@SysLog("删除角色")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:role:delete")
	public R delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);
		return R.ok();
	}
}